<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use common\models\datas\Customer;
use Yii;

/**
 * This is the model class for table "{{%complaint}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $mechanic_id
 * @property integer $service_level
 * @property integer $teaching_level
 * @property integer $environmental_level
 * @property integer $status
 * @property string $complaint_content
 * @property integer $admin_id
 * @property string $admin_name
 * @property string $remarks
 * @property integer $created_at
 * @property integer $updated_at
 */
class Complaint extends \zhtfrontend\components\bases\BaseModel
{

    public $new_remark;
    public $new_complaint_content;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%complaint}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'service_level', 'teaching_level', 'environmental_level', 'status', 'admin_id', 'created_at', 'updated_at','is_satisfied'], 'integer'],
            [['complaint_content', 'remarks','new_remark','new_complaint_content'], 'string'],
            [['admin_name'], 'string', 'max' => 50],
            [['mechanic_id'],'check_mechanic','skipOnEmpty'=>false,'on'=>'wechat_complaint'],
            [['complaint_content'],'string', 'min'=>20,'on'=>'wechat_complaint'],
            [['status'],'required'],
            [['new_remark'],'remarks_required','skipOnEmpty'=>false,'on' => ['handle']],
            [['service_level', 'teaching_level', 'environmental_level'],'check_select','skipOnEmpty'=>false,'on' => ['wechat_complaint']]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'customer_id' => Yii::t('zhtfrontend', '会员'),
            'mechanic_id' => Yii::t('zhtfrontend', '机构'),
            'service_level' => Yii::t('zhtfrontend', '服务投诉'),
            'teaching_level' => Yii::t('zhtfrontend', '教学投诉'),
            'environmental_level' => Yii::t('zhtfrontend', '环境投诉'),
            'status' => Yii::t('zhtfrontend', '状态'),
            'complaint_content' => Yii::t('zhtfrontend', '投诉内容'),
            'admin_id' => Yii::t('zhtfrontend', '处理人id'),
            'admin_name' => Yii::t('zhtfrontend', '处理人员姓名'),
            'remarks' => Yii::t('zhtfrontend', '历史备注'),
            'created_at' => Yii::t('zhtfrontend', '投诉时间'),
            'updated_at' => Yii::t('zhtfrontend', '处理时间'),
            'new_remark' => Yii::t('zhtfrontend', '备注'),
            'is_satisfied' => Yii::t('zhtfrontend', '用户是否满意'),
            'new_complaint_content' => Yii::t('zhtfrontend', '追加内容'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['updated_at'] = function () {
            if($this->updated_at >0)
                return \Yii::$app->formatter->asDatetime($this->updated_at);
            else
                return Yii::t('zhtfrontend','未处理');
        };
        $fields['admin_name'] = function () {
            if($this->admin_name == null)
                return Yii::t('zhtfrontend','未处理');
            else
                return $this->admin_name;

        };
        $fields['status'] = function () {
            switch($this->status){
                case 0:
                    return Yii::t('zhtfrontend','已提交');
                case 1:
                    return Yii::t('zhtfrontend','处理中');
                case 2:
                    return Yii::t('zhtfrontend','处理完成');
                case 3:
                    return Yii::t('zhtfrontend','处理失败');
            }
        };
        $fields['is_satisfied'] = function () {
            switch($this->is_satisfied){
                case 0:
                    return Yii::t('zhtfrontend','--');
                case 1:
                    return Yii::t('zhtfrontend','满意');
                case 2:
                    return Yii::t('zhtfrontend','不满意');
            }
        };
        $fields['mechanic_name'] = function () {
            return isset($this->mechanic)?$this->mechanic->name:'';
        };
        $fields['href'] = function () {
            return \Yii::$app->params['wechat_url'].'/complaint/list';
        };
        return $fields;
    }
    public function extraFields(){
        return [
            'customer_name' => function(){
                 return isset($this->customer)?$this->customer->name:'';
            },
            'mechanic_name' => function(){
                return isset($this->mechanic)?$this->mechanic->name:'';
            },
            'mobile' => function(){
                return isset($this->customer)?$this->customer->mobile:'';
            },
            'operate' => function () {
//                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                        'handle' => function ($data) {
                            if ($data['is_satisfied'] == 1 || (in_array($data['status'],[2,3]) && $data['is_satisfied'] == 0)) {
                                return '<button class="yx-btn-disabled" type="button">完成</button>';
                            } else {
                                return '';
                            }
                        },
                        'view'=>function(){
                            return '';
                        }
                    ]
                )->create();
            },
        ];
    }

    public function remarks_required($attribute){
        if($this->status > 0 && $this->$attribute == ''){
                $this->addError($attribute, Yii::t('zhtfrontend', '处理备注不能为空'));
        }
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }
    public function getMechanic(){
        return $this->hasOne(Mechanic::className(),['id'=>'mechanic_id']);
    }

    public function check_mechanic($attribute){
        if(empty($this->$attribute)){
            $this->addError($attribute, '机构名称不能为空');
        }else{
            $mechanic=Mechanic::find()->where(['name'=>$this->$attribute])->one();
            if(!isset($mechanic)){
                $this->addError($attribute, '请正确填写机构！');
            }else{
                $customerId=\Yii::$app->user->id;
                $complaint=Complaint::find()->where(['mechanic_id'=>$mechanic->id,'customer_id'=>$customerId])->one();
                if(isset($complaint)){
                    $this->addError($attribute, '您对该机构投诉，我们会尽快处理，不能再次投诉！');
                }
            }
        }
    }
    
    public function check_select(){
        if($this->service_level==null&&$this->teaching_level==null&&$this->environmental_level==null){
            $this->addError('service_level', '请选择您至少选择一项要投诉的选项！');
        }
    }

}
